package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.Button;
import android.widget.TextView;

import com.sphd.Restaurant.R;
import com.sphd.Restaurant.adapters.OrderAdapter;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.fragments.OrderItemFragment;
import com.sphd.Restaurant.infos.Dishes;
import com.sphd.Restaurant.infos.RestaurantInfo;
import com.sphd.Restaurant.infos.Sorts;
import com.sphd.Restaurant.infos.VipInfo;
import com.sphd.Restaurant.interfFace.TrueMoneyCallBack;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.OrderP;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.OderView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderActivity extends BaseActivity implements OderView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.my_viewPager)
    ViewPager myViewPager;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_allmoney)
    TextView tvOrderAllmoney;
    @BindView(R.id.bt_order_ok)
    Button btOrderOk;
    Intent intent;
    Bundle bundle;
    OrderP orderp;
    RestaurantInfo restInfo;
    int type;
    OrderAdapter adapter;
    int number;
    double mymoney;
    List<Dishes> dishes;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        setToolBar(toolbar, "点餐", true);
        restInfo = (RestaurantInfo) bundle.getSerializable(Constants.RESTAURANT);
        type = bundle.getInt(Constants.TYPE);
        orderp = new OrderP(this);
        orderp.addData(Model.HTTPURL + Model.ALLGREENS + "cgid=" + restInfo.getId() + "&nw=" + type);
        toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(this));
    }

    @Override
    public void sendLists(List<Sorts> sortses, List<Dishes> dishes) {
        this.dishes=dishes;
        List<OrderItemFragment> fragments = new ArrayList<>();
        List<Sorts> newSorts = new ArrayList<>();
        for (Sorts sort : sortses) {
            if (getGreensList(sort.getSortno(), dishes).size() != 0) {
                newSorts.add(sort);
                OrderItemFragment fragment = new OrderItemFragment();
                fragment.setaddCallBack(new AddCallback());
                fragment.setRemoveCallBack(new RemoveCallBack());
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constants.RESTAURANT, restInfo);
                bundle.putSerializable(Constants.DISHES, (Serializable) getGreensList(sort.getSortno(), dishes));
                fragment.setArguments(bundle);
                fragments.add(fragment);
            }
        }
        adapter = new OrderAdapter(getSupportFragmentManager(), fragments, newSorts);
        myViewPager.setAdapter(adapter);
        tabs.setupWithViewPager(myViewPager);
        if (sortses.size() >= 5) {
            tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        } else {
            tabs.setTabMode(TabLayout.MODE_FIXED);
        }
    }

    /**
     * 根据no 获取本菜品种类的所有菜品
     *
     * @param no
     * @param dishes
     * @return
     */
    private List<Dishes> getGreensList(String no, List<Dishes> dishes) {
        List<Dishes> list = new ArrayList<>();
        for (int i = 0; i < dishes.size(); i++) {
            if (dishes.get(i).getSortno().equals(no)) {
                list.add(dishes.get(i));
            }
        }
        return list;
    }

    @Override
    public void showdialog() {

    }

    @Override
    public void dissmissdialog() {

    }

    @OnClick(R.id.bt_order_ok)
    public void onClick() {
        if (number == 0) {
            showAlerDialog(1, "请点餐后确认下单");
        } else {
            orderp.checklimit(Model.HTTPURL + Model.ISVIP + "username=" + getString(Constants.USERPHONE) + "&cgid=" + restInfo.getId());
        }


    }

    /**
     * 当fragment中item点击＋时回调修改activity中金额
     */
    public class AddCallback implements TrueMoneyCallBack {

        @Override
        public void sendcallback(double money) {
            number++;
            mymoney = mymoney + money;
            tvOrderNum.setText("已点" + number + "份餐品");
            toSetText();
        }
    }

    /**
     * 当fragment中item点击－时回调修改activity中金额
     */
    public class RemoveCallBack implements TrueMoneyCallBack {
        @Override
        public void sendcallback(double money) {
            number--;
            mymoney = mymoney - money;
            tvOrderNum.setText("已点" + number + "份餐品");
            if (number == 0) {
                tvOrderAllmoney.setText("共计￥0元");
            } else {
                toSetText();
            }
        }
    }

    private void toSetText() {
        tvOrderAllmoney.setText("共计￥" + turn(mymoney, 1) + "元");
    }

    @Override
    public void sendVipList(List<VipInfo> vipInfos) {
        if (vipInfos.size() == 0) {
            showAlerDialog(1, "请先加入会员");
        } else {
            bundle.putInt(Constants.DISHESNO,number);
            bundle.putSerializable(Constants.VIPINFO, (Serializable) vipInfos);
            bundle.putSerializable(Constants.ORDERLIST, (Serializable) confrimGreens(dishes));
            startActivity(AffirmOrderActivity.class,bundle);
        }
    }

    /**
     * 将菜品列表中用户选择了的汇总在一个列表中
     *
     * @param allOrder
     * @return
     */
    private List<Dishes> confrimGreens(List<Dishes> allOrder) {
        List<Dishes> infos = new ArrayList<>();
        for (int i = 0; i < allOrder.size(); i++) {
            if (allOrder.get(i).getOrder_no() != 0) {
                infos.add(allOrder.get(i));
            }
        }
        return infos;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
    }
}
